<?php

include('db_connection.php');

class User_subscribe {

    function saveUserDetail() {
        if (trim($_POST['user_email']) != '' && trim($_POST['user_name']) != '') {
            $select_subscribe = "SELECT * FROM tbl_user WHERE email='" . $_POST['user_email'] . "'";
            $query_subscribe = mysql_query($select_subscribe) or die(mysql_error());

            $count_row_subscribe = mysql_num_rows($query_subscribe);

            if ($count_row_subscribe > 0) {
                $user_id = '';
                $act_key = '';
                $is_active = '0';
                $user_name = '';
                $user_email = '';
                $count = '';

                while ($data_array = mysql_fetch_array($query_subscribe)) {
                    $id = $data_array['id'];
                    $act_key = $data_array['activate_link'];
                    $is_active = $data_array['is_active'];
                    $user_name = $data_array['username'];
                    $user_email = $data_array['email'];
                    $count = $data_array['count'];
                    ;
                }

                $activate_url_msg = '';

                if ($is_active) {
                    $count = $count + 1;

                    $update = "UPDATE tbl_user SET count='" . $count . "',update_ts=now() WHERE id='" . $id . "'";
                    $user_update = mysql_query($update) or die(mysql_error());
                } else {
                    $activate_url_msg = "Please Activate Your Account. Activation Link is in your Email";
                }
                return $activate_url_msg;
            } else {
                $unique = date('Y-m-d H:i:s');
                $unique = md5($unique);
                $count = 0;
                $reference_value = $this->createRandomStringForReference();

                $ref_by = '';
                if (isset($_SESSION['reffered_by'])) {
                    $ref_by = 'a_' . $_SESSION['reffered_by'];
                }

                $insert = "INSERT INTO tbl_user (username,email,reference_url,reffered_by) VALUES ('" . $_POST['user_name'] . "','" . $_POST['user_email'] . "','" . $reference_value . "','" . $ref_by . "')";
                $user_insert = mysql_query($insert) or die(mysql_error());
                $last_id = mysql_insert_id();
                $activate_url = 'http://' . $_SERVER['SERVER_NAME'] . '/classess/user_activate.php?id=' . $last_id . '&uic=' . $unique;



                $update = "UPDATE tbl_user SET activate_link='" . $unique . "' WHERE id='" . $last_id . "'";
                $user_update = mysql_query($update) or die(mysql_error());
                $send_msg = 'Please Activate Your Account.<br/>Your Activation Link is <a href="' . $activate_url . '">"' . $activate_url . '"</a>';
					
                $activate_url_msg = "Please Activate Your Account. Activation Link is in your Email";

                if (isset($_SESSION['reffered_by'])) {
                    $_SESSION['reffered_by'] = ' ';
                    unset($_SESSION['reffered_by']);
                }

                /* This Code is used for send Email. Start Here */
                
                $activate_url = $activate_url;
                $user_name = $_POST['user_name'];
                $user_mail = $_POST['user_email'];
              //  $password = $random_string_final;
                ob_start();
				//echo 'test1';exit;
                include_once('mailtemplate/accout_activate.php');
                $mail_body = ob_get_contents();
                ob_end_clean();    
                      #echo $mail_body;exit;                                        
                                                                
                $mail_to = $_POST['user_email'];
                $mail_subject = "Email Activation";
              //  $mail_body = "This is the email to activate your account.<br/>\n";
               // $mail_body.="Click the following link to activate your account.<br/>\n";
               // $mail_body.="<a href='" . $activate_url . "'>Click here</a>";
                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                $sent = mail($mail_to, $mail_subject, $mail_body, $headers);

                $mail_to = 'Vipin7477@gmail.com';
                $mail_subject = "Email Activation";
               // $mail_body = "This is the email to activate your account.<br/>\n";
              //  $mail_body.="Click the following link to activate your account.<br/>\n";
              //  $mail_body.="<a href='" . $activate_url . "'>Click here</a>";
                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                $sent = mail($mail_to, $mail_subject, $mail_body, $headers);

                /* This Code is used for send Email. End Here */
                return $activate_url_msg;
            }
        } else {
            $val_msg = '';
            if (trim($_POST['user_email']) == '' && trim($_POST['user_name']) == '') {
                $val_msg = 'Name And Email Is Required';
            } else if (trim($_POST['user_email']) == '') {
                $val_msg = 'Email Is Required';
            } else if (trim($_POST['user_name']) == '') {
                $val_msg = 'Name Is Required';
            }
            return $val_msg;
        }
    }

    function saveUserInfoAfterBilling() {
        $random_string_final = '';
        $pwd_row = '';
        $alpha_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        $numeric_arr = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

        function array_random_assoc($arr, $num = 1) {
            $keys = array_keys($arr);
            shuffle($keys);

            $r = '';
            for ($i = 0; $i < $num; $i++) {
                $r .= $arr[$keys[$i]];
            }
            return $r;
        }

        $count = 1;
        while ($count < 2) {
            $random_alpha_string = array_random_assoc($alpha_arr, 4);
            $random_numeric_string = array_random_assoc($numeric_arr, 4);
            $random_string = $random_alpha_string . $random_numeric_string;
            $random_string = str_shuffle($random_string);


            $select = "SELECT password FROM tbl_user WHERE password='" . md5($random_string) . "'";

            $query = mysql_query($select);
            $row = mysql_num_rows($query);
            if (!$row) {
                $count = 2;
                $random_string_final = $random_string;
            }
        }

        if ($count == 2) {
            $select_subscribe = "SELECT * FROM tbl_user WHERE email='" . $_POST['billing_user_email'] . "'";
            $query_subscribe = mysql_query($select_subscribe) or die(mysql_error());

            $count_row_subscribe = mysql_num_rows($query_subscribe);

            if ($count_row_subscribe > 0) {
                ob_start();
                while ($row = mysql_fetch_array($query_subscribe)) {
                    $row_chk_pwd = " ";
                    $set_eml_pwd = "";
                    $upd_ts = ', update_ts=now()';
                    if ($row['password']) {
                        $row_chk_pwd = " ";
                        $set_eml_pwd = "";
                    } else {
                        if (!isset($_POST['is_ebook_chk'])) {
                            $row_chk_pwd = "";
                            $set_eml_pwd = "&prv_msg_set=1";
                            $upd_ts = '';

                            /* This Code is used for send Email. Start Here */
                            /* $mail_to=$_POST['billing_user_email'];
                              $mail_subject="User Name And Password";
                              $mail_body = "This is the email for forwarding email And Password.<br/>\r\n";
                              $mail_body.="Login URL = <a href='http://rvtraining.net/traineeAdmin/'>http://rvtraining.net/traineeAdmin</a> <br/>\r\n";
                              $mail_body.="User Name = ".$_POST['billing_user_email']."<br/>\r\n";
                              $mail_body.="Password = ".$random_string_final."<br/>\r\n";
                              $headers  = 'MIME-Version: 1.0' . "\r\n";
                              $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                              $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                              @mail($mail_to,$mail_subject,$mail_body, $headers); */
                            /* This Code is used for send Email. End Here */
                        }
                    }

                    $is_ebook_value = 0;
                    $ebook_download_time_val = 0;
                    if (isset($_POST['is_ebook_chk'])) {
                        $is_ebook_value = 1;
                        $ebook_download_time_val = 1;
                    }

                    $insert_userInfo = "INSERT INTO tbl_user_details (user_id,name,course_id,course_fee,address,city,state,zip,country,phone,skills,is_ebook,ebook_download_time) VALUES('" . $row['id'] . "','" . $_POST['billing_user_name'] . "','" . $_SESSION['course_id'] . "','" . $_SESSION['course_fee'] . "','" . $_POST['billing_user_address'] . "','" . $_POST['billing_user_city'] . "','" . $_POST['billing_user_state'] . "','" . $_POST['billing_user_zip'] . "','" . $_POST['billing_user_country'] . "','" . $_POST['billing_user_phone'] . "','" . $_POST['user_skills'] . "','" . $is_ebook_value . "','" . $ebook_download_time_val . "')"; 
                    $user_insert = mysql_query($insert_userInfo) or die(mysql_error());
                    $last_user_detials_id = mysql_insert_id();

                    /* if(!$row['password']){
                      if(!isset($_POST['is_ebook_chk'])){
                      $update = "UPDATE tbl_user SET ".$row_chk_pwd." WHERE id='".$row['id']."'";
                      $user_update = mysql_query($update) or die(mysql_error());
                      }
                      } */
                    $ebook_crs_name = '';

                    if (isset($_POST['is_ebook_chk'])) {
                        $select_course = "SELECT * FROM tbl_ebook WHERE id='" . $_SESSION['course_id'] . "'";
                        $query_course = mysql_query($select_course) or die(mysql_error());

                        if (mysql_num_rows($query_course) > 0) {
                            while ($row_course = mysql_fetch_array($query_course)) {
                                $ebook_crs_name = $row_course['ebook_name'];
                            }
                        }
                    } else {
                        $select_course = "SELECT * FROM tbl_courses WHERE id='" . $_SESSION['course_id'] . "'";
                        $query_course = mysql_query($select_course) or die(mysql_error());

                        if (mysql_num_rows($query_course) > 0) {
                            while ($row_course = mysql_fetch_array($query_course)) {
                                $ebook_crs_name = $row_course['course_name'];
                            }
                        }
                    }


                    /* This Code is used for send Email. Start Here */
                    
                    
                    $activate_url = $ebook_crs_name;
                    $user_name = $_POST['billing_user_name'];
                    $user_mail = $_POST['billing_user_email'];
                    $password = $random_string_final;
                    ob_start();
                    include_once('mailtemplate/subscription_activate.php');
                    $mail_body = ob_get_contents();
                    ob_end_clean(); 

                
                    $mail_to = $_POST['billing_user_email'];
                    $mail_subject = "Rv Training Thanks Message";
                  //  $mail_body = "Thanks For Subscribing the " . $ebook_crs_name . ".<br/>\r\n";
                    $headers = 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                    $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                    mail($mail_to, $mail_subject, $mail_body, $headers);

                    $mail_to = 'Vipin7477@gmail.com';
                    $mail_subject = "Rv Training Thanks Message";
                    //$mail_body = "Thanks For Subscribing the " . $ebook_crs_name . ".<br/>\r\n";
                    $headers = 'MIME-Version: 1.0' . "\r\n";
                    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                    $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                    mail($mail_to, $mail_subject, $mail_body, $headers);
                    /* This Code is used for send Email. End Here */

                    if (isset($_POST['pm_method_bank'])) {
                        $update_pd_status = "UPDATE tbl_user_details SET via_paid='bank' WHERE id='" . $last_user_detials_id . "'";
                        $user_update_pd_status = mysql_query($update_pd_status) or die(mysql_error());

                        $insert_bnk_method = "INSERT INTO tbl_user_bnk_details (user_id,user_detail_id,course_id,bank_name,branch_name,account_number,date) VALUES ('" . $row['id'] . "','" . $last_user_detials_id . "','" . $_SESSION['course_id'] . "','" . $_POST['billing_user_bank_name'] . "','" . $_POST['billing_user_branch_name'] . "','" . $_POST['billing_user_acc_num'] . "','" . $_POST['billing_user_date'] . "')";
                        $query_bnk_method = mysql_query($insert_bnk_method) or die(mysql_error());
                    }

                    if (isset($_POST['pm_method_paypal'])) {
                        $_SESSION['last_user_detail_id'] = $last_user_detials_id;
                        $_SESSION['last_user_id'] = $row['id'];
                        include('paypal_entry.php');
                        /* $update_pd_status = "UPDATE tbl_user_details SET via_paid='paypal' WHERE id='".$last_user_detials_id."'";
                          $user_update_pd_status = mysql_query($update_pd_status) or die(mysql_error());

                          $update = "UPDATE tbl_user SET is_paid='1', is_active=1 WHERE id='".$row['id']."'";
                          $user_update = mysql_query($update) or die(mysql_error());

                          $insert_cart = "INSERT INTO tbl_cart_payment (user_id,course_id,user_detail_id,payment) values('".$row['id']."','".$_SESSION['course_id']."','".$last_user_detials_id."','".$_SESSION['course_fee']."')";
                          $query_insert_cart = mysql_query($insert_cart) or die(mysql_error()); */
                    }

                    if ($row['reffered_by']) {
                        $reffrel_id = explode('_', $row['reffered_by']);
                        $email_reffrel = '';
                        $percent_comm = '';
                        $table = '';
                        $course_fee = '';

                        $select_user_admin_mail = "SELECT * FROM tbl_affiliate WHERE id = '" . $reffrel_id[1] . "'";
                        $query_user_admin_mail = mysql_query($select_user_admin_mail) or die(mysql_error());
                        if (mysql_num_rows($query_user_admin_mail) > 0) {
                            while ($row_admin_mail = mysql_fetch_array($query_user_admin_mail)) {
                                $email_reffrel = $row_admin_mail['email'];
                            }
                        }

                        $select_commission = "SELECT * FROM tbl_commission WHERE user_admin_id = '" . $row['reffered_by'] . "' AND course_id='" . $_SESSION['course_id'] . "'";
                        $query_commission = mysql_query($select_commission) or die(mysql_error());

                        if (mysql_num_rows($query_commission) > 0) {
                            while ($row_commission = mysql_fetch_array($query_commission)) {
                                $percent_comm = $row_commission['commission_percentage'];
                            }
                            $course_fee = $_SESSION['course_fee'];
                        } else {
                            $select_course = "SELECT * FROM tbl_courses WHERE id='" . $_SESSION['course_id'] . "'";
                            $query_course = mysql_query($select_course) or die(mysql_error());

                            if (mysql_num_rows($query_course) > 0) {
                                while ($row_course = mysql_fetch_array($query_course)) {
                                    $percent_comm = $row_course['course_commission'];
                                }
                            }

                            $course_fee = $_SESSION['course_fee'];
                        }

                        if ($percent_comm) {
                            $commision_fee = $course_fee * $percent_comm;
                            $total_commision = $commision_fee / 100;

                            $insert_commission = "INSERT INTO tbl_commission (user_admin_id,course_id,trainee_id,trainee_detail_id,commission_percentage,commission,commission_paid_status) VALUES ('" . $row['reffered_by'] . "','" . $_SESSION['course_id'] . "','" . $row['id'] . "','" . $last_user_detials_id . "','" . $percent_comm . "','" . $total_commision . "','0')";
                            $query_commission = mysql_query($insert_commission) or die(mysql_error());
                        }
                    }
                    //header('location:index.php?home=1'.$set_eml_pwd);
                    //die("<script>location.href = 'index.php?home=1" . $set_eml_pwd . "'</script>");
                    die("<script>location.href = 'thank_you.php?user_id=".$last_user_detials_id."'</script>");
                }
            } else {
                $unique = date('Y-m-d H:i:s');
                $unique = md5($unique);
                $count = 0;
                $reference_value = $this->createRandomStringForReference();

                $ref_by = '';
                if (isset($_SESSION['reffered_by'])) {
                    $ref_by = 'a_' . $_SESSION['reffered_by'];
                }

                if (!isset($_POST['is_ebook_chk'])) {
                    $insert = "INSERT INTO tbl_user (username,email,reference_url,is_active,reffered_by) VALUES ('" . $_POST['billing_user_name'] . "','" . $_POST['billing_user_email'] . "','" . $reference_value . "','1','" . $ref_by . "')";
                } else {
                    $insert = "INSERT INTO tbl_user (username,email,reference_url,is_active,reffered_by) VALUES ('" . $_POST['billing_user_name'] . "','" . $_POST['billing_user_email'] . "','" . $reference_value . "','1','" . $ref_by . "')";
                }
                $user_insert = mysql_query($insert) or die(mysql_error());
                $last_id = mysql_insert_id();
                $activate_url = 'http://' . $_SERVER['SERVER_NAME'] . '/classess/user_activate.php?id=' . $last_id . '&uic=' . $unique;

                $update = "UPDATE tbl_user SET activate_link='" . $unique . "' WHERE id='" . $last_id . "'";
                $user_update = mysql_query($update) or die(mysql_error());

                $is_ebook_value = 0;
                $ebook_download_time_val = 0;
                if (isset($_POST['is_ebook_chk'])) {
                    $is_ebook_value = 1;
                    $ebook_download_time_val = 1;
                }

                 $insert_userInfo = "INSERT INTO tbl_user_details (user_id,name,course_id,course_fee,address,city,state,zip,country,phone,skills,is_ebook,ebook_download_time) VALUES('" . $last_id . "','" . $_POST['billing_user_name'] . "','" . $_SESSION['course_id'] . "','" . $_SESSION['course_fee'] . "','" . $_POST['billing_user_address'] . "','" . $_POST['billing_user_city'] . "','" . $_POST['billing_user_state'] . "','" . $_POST['billing_user_zip'] . "','" . $_POST['billing_user_country'] . "','" . $_POST['billing_user_phone'] . "','" . $_POST['user_skills'] . "','" . $is_ebook_value . "','" . $ebook_download_time_val . "')"; 
                $user_insert = mysql_query($insert_userInfo) or die(mysql_error());
                $last_user_detials_id = mysql_insert_id();

                $send_msg = 'Your Email=' . $_POST['billing_user_email'] . ' And Password = ' . $random_string_final;

                if (isset($_POST['is_ebook_chk'])) {
                    /* $mail_to=$_POST['billing_user_email'];
                      $mail_subject="User Name And Password";
                      $mail_body = "This is the email for forwarding email And Password.<br/>\r\n";
                      $mail_body.="Login URL = <a href='http://rvtraining.net/traineeAdmin/'>http://rvtraining.net/traineeAdmin</a> <br/>\r\n";
                      $mail_body.="User Name = ".$_POST['billing_user_email']."<br/>\r\n";
                      $mail_body.="Password = ".$random_string_final."<br/>\r\n";
                      $headers  = 'MIME-Version: 1.0' . "\r\n";
                      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                      $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                      mail($mail_to,$mail_subject,$mail_body, $headers); */
                }

                $ebook_crs_name = '';
                if (isset($_POST['is_ebook_chk'])) {
                    $select_course = "SELECT * FROM tbl_ebook WHERE id='" . $_SESSION['course_id'] . "'";
                    $query_course = mysql_query($select_course) or die(mysql_error());

                    if (mysql_num_rows($query_course) > 0) {
                        while ($row_course = mysql_fetch_array($query_course)) {
                            $ebook_crs_name = $row_course['ebook_name'];
                        }
                    }
                } else {
                    $select_course = "SELECT * FROM tbl_courses WHERE id='" . $_SESSION['course_id'] . "'";
                    $query_course = mysql_query($select_course) or die(mysql_error());

                    if (mysql_num_rows($query_course) > 0) {
                        while ($row_course = mysql_fetch_array($query_course)) {
                            $ebook_crs_name = $row_course['course_name'];
                        }
                    }
                }

                /* This Code is used for send Email. Start Here */
                
                 $activate_url = $ebook_crs_name;
                    $user_name = $_POST['billing_user_name'];
                    $user_mail = $_POST['billing_user_email'];
                    $password = $random_string_final;
                    ob_start();
                    include_once('mailtemplate/subscription_activate.php');
                    $mail_body = ob_get_contents();
                    ob_end_clean(); 
                    
                    
                $mail_to = $_POST['billing_user_email'];
                $mail_subject = "Rv Training Thanks Message";
             //   $mail_body = "Thanks For Subscribing the " . $ebook_crs_name . ".<br/>\r\n";
                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                mail($mail_to, $mail_subject, $mail_body, $headers);

                $mail_to = 'Vipin7477@gmail.com';
                $mail_subject = "Rv Training Thanks Message";
             //   $mail_body = "Thanks For Subscribing the " . $ebook_crs_name . ".<br/>\r\n";
                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                mail($mail_to, $mail_subject, $mail_body, $headers);
                /* This Code is used for send Email. End Here */

                if (isset($_POST['pm_method_bank'])) {
                    $update_pd_status = "UPDATE tbl_user_details SET via_paid='bank' WHERE id='" . $last_user_detials_id . "'";
                    $user_update_pd_status = mysql_query($update_pd_status) or die(mysql_error());

                    $insert_bnk_method = "INSERT INTO tbl_user_bnk_details (user_id,user_detail_id,course_id,bank_name,branch_name,account_number,date) VALUES ('" . $last_id . "','" . $last_user_detials_id . "','" . $_SESSION['course_id'] . "','" . $_POST['billing_user_bank_name'] . "','" . $_POST['billing_user_branch_name'] . "','" . $_POST['billing_user_acc_num'] . "','" . $_POST['billing_user_date'] . "')";
                    $query_bnk_method = mysql_query($insert_bnk_method) or die(mysql_error());
                }

                if (isset($_POST['pm_method_paypal'])) {
                    $_SESSION['last_user_detail_id'] = $last_user_detials_id;
                    $_SESSION['last_user_id'] = $last_id;
                    include('paypal_entry.php');
                    /* $update_pd_status = "UPDATE tbl_user_details SET via_paid='paypal' WHERE id='".$last_user_detials_id."'";
                      $user_update_pd_status = mysql_query($update_pd_status) or die(mysql_error());

                      $update = "UPDATE tbl_user SET is_paid='1' WHERE id='".$last_id."'";
                      $user_update = mysql_query($update) or die(mysql_error());

                      $insert_cart = "INSERT INTO tbl_cart_payment (user_id,course_id,user_detail_id,payment) values('".$last_id."','".$_SESSION['course_id']."','".$last_user_detials_id."','".$_SESSION['course_fee']."')";
                      $query_insert_cart = mysql_query($insert_cart) or die(mysql_error()); */
                }

                if (isset($_SESSION['reffered_by'])) {
                    $email_reffrel = '';
                    $percent_comm = '';
                    $table = '';
                    $course_fee = '';

                    $select_user_admin_mail = "SELECT * FROM tbl_affiliate WHERE id = '" . $_SESSION['reffered_by'] . "'";
                    $query_user_admin_mail = mysql_query($select_user_admin_mail) or die(mysql_error());
                    if (mysql_num_rows($query_user_admin_mail) > 0) {
                        while ($row_admin_mail = mysql_fetch_array($query_user_admin_mail)) {
                            $email_reffrel = $row_admin_mail['email'];
                        }
                    }

                    $select_commission = "SELECT * FROM tbl_commission WHERE user_admin_id = 'a_" . $_SESSION['reffered_by'] . "' AND course_id='" . $_SESSION['course_id'] . "'";
                    $query_commission = mysql_query($select_commission) or die(mysql_error());

                    if (mysql_num_rows($query_commission) > 0) {
                        while ($row_commission = mysql_fetch_array($query_commission)) {
                            $percent_comm = $row_commission['commission_percentage'];
                        }
                        $course_fee = $_SESSION['course_fee'];
                    } else {
                        $select_course = "SELECT * FROM tbl_courses WHERE id='" . $_SESSION['course_id'] . "'";
                        $query_course = mysql_query($select_course) or die(mysql_error());

                        if (mysql_num_rows($query_course) > 0) {
                            while ($row_course = mysql_fetch_array($query_course)) {
                                $percent_comm = $row_course['course_commission'];
                            }
                        }

                        $course_fee = $_SESSION['course_fee'];
                    }

                    if ($percent_comm) {
                        $commision_fee = $course_fee * $percent_comm;
                        $total_commision = $commision_fee / 100;

                        $insert_commission = "INSERT INTO tbl_commission (user_admin_id,course_id,trainee_id,trainee_detail_id,commission_percentage,commission,commission_paid_status) VALUES ('a_" . $_SESSION['reffered_by'] . "','" . $_SESSION['course_id'] . "','" . $last_id . "','" . $last_user_detials_id . "','" . $percent_comm . "','" . $total_commision . "','0')";
                        $query_commission = mysql_query($insert_commission) or die(mysql_error());
                    }
                    $_SESSION['reffered_by'] = ' ';
                    unset($_SESSION['reffered_by']);
                }

                //header('location:index.php?home=1');		
                //die("<script>location.href = 'index.php?home=1&prv_msg_set=1'</script>");
                die("<script>location.href = 'thank_you.php?user_id=".$last_user_detials_id."'</script>");
            }
        }
    }

    function saveUserDetailWithReffrel() {
        if (trim($_POST['user_email']) != '' && trim($_POST['user_name']) != '') {
            $select_subscribe = "SELECT * FROM tbl_user WHERE email='" . $_POST['user_email'] . "'";
            $query_subscribe = mysql_query($select_subscribe) or die(mysql_error());

            $count_row_subscribe = mysql_num_rows($query_subscribe);

            if ($count_row_subscribe > 0) {
                while ($data_array = mysql_fetch_array($query_subscribe)) {
                    $is_active = $data_array['is_active'];
                }

                $activate_url_msg = '';

                if ($is_active) {
                    $activate_url_msg = "This User Is Already Exist";
                } else {
                    $activate_url_msg = "Please Activate Your Account. Activation Link is in your Email";
                }
                return $activate_url_msg;
            } else {
                $unique = date('Y-m-d H:i:s');
                $unique = md5($unique);
                $count = 0;
                $reference_value = $this->createRandomStringForReference();

                $insert = "INSERT INTO tbl_user (username,email,reference_url,reffered_by) VALUES ('" . $_POST['user_name'] . "','" . $_POST['user_email'] . "','" . $reference_value . "','a_" . $_SESSION['reffered_by'] . "')";
                $user_insert = mysql_query($insert) or die(mysql_error());
                $last_id = mysql_insert_id();
                $activate_url = 'http://' . $_SERVER['SERVER_NAME'] . '/classess/user_activate.php?id=' . $last_id . '&uic=' . $unique;

                $update = "UPDATE tbl_user SET activate_link='" . $unique . "' WHERE id='" . $last_id . "'";
                $user_update = mysql_query($update) or die(mysql_error());

                $activate_url_msg = "Please Activate Your Account. Activation Link is in your Email";


                $_SESSION['reffered_by'] = ' ';
                unset($_SESSION['reffered_by']);

                /* This Code is used for send Email. Start Here */
                
                    $activate_url = $activate_url;
                    $user_name = $_POST['user_name'];
                    $user_mail = $_POST['user_email'];
                    ob_start();
					 //echo 'test11';exit;
                    include_once('mailtemplate/accout_activate.php');
                    $mail_body = ob_get_contents();
                    ob_end_clean(); 
                   
                    
                    
                $mail_to = $_POST['user_email'];
                $mail_subject = "Email Activation";
              //  $mail_body = "This is the email to activate your account.<br/>\n";
              //  $mail_body.="Click the following link to activate your account.<br/>\n";
              //  $mail_body.="<a href='" . $activate_url . "'>Click here</a>";
                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                $sent = mail($mail_to, $mail_subject, $mail_body, $headers);

                $mail_to = 'Vipin7477@gmail.com';
                $mail_subject = "Email Activation";
           //     $mail_body = "This is the email to activate your account.<br/>\n";
           //     $mail_body.="Click the following link to activate your account.<br/>\n";
           //     $mail_body.="<a href='" . $activate_url . "'>Click here</a>";
                $headers = 'MIME-Version: 1.0' . "\r\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
                $sent = mail($mail_to, $mail_subject, $mail_body, $headers);

                /* This Code is used for send Email. End Here */
                return $activate_url_msg;
            }
        } else {
            $val_msg = '';
            if (trim($_POST['user_email']) == '' && trim($_POST['user_name']) == '') {
                $val_msg = 'Name And Email Is Required';
            } else if (trim($_POST['user_email']) == '') {
                $val_msg = 'Email Is Required';
            } else if (trim($_POST['user_name']) == '') {
                $val_msg = 'Name Is Required';
            }
            return $val_msg;
        }
    }

    function createRandomStringForReference() {
        $random_string_final = '';
        $pwd_row = '';
        $alpha_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        $numeric_arr = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

        $count = 1;
        while ($count < 2) {
            $random_alpha_string = $this->array_random_assoc($alpha_arr, 5);
            $random_numeric_string = $this->array_random_assoc($numeric_arr, 5);
            $random_string = $random_alpha_string . $random_numeric_string;
            $random_string = str_shuffle($random_string);


            $select_user = "SELECT reference_url FROM tbl_user WHERE reference_url='" . $random_string . "'";
            $query_user = mysql_query($select_user);
            $row_user = mysql_num_rows($query_user);

            $select_admin = "SELECT reference_url FROM tbl_admin WHERE reference_url='" . $random_string . "'";
            $query_admin = mysql_query($select_admin);
            $row_admin = mysql_num_rows($query_admin);

            if (!$row_user && !$row_admin) {
                $count = 2;
                $random_string_final = $random_string;
            }
        }

        if ($count == 2) {
            return $random_string_final;
        }
    }

    function array_random_assoc($arr, $num = 1) {
        $keys = array_keys($arr);
        shuffle($keys);

        $r = '';
        for ($i = 0; $i < $num; $i++) {
            $r .= $arr[$keys[$i]];
        }
        return $r;
    }

}

?>